home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / gawk / cawf2st.zoo / bsfilt.c < prev    next >
C/C++ Source or Header  |  1992-04-12  |  6KB  |  207 lines

  1. /*
  2.  *      bsfilt.c - a colcrt-like processor for cawf(1)
  3.  */
  4.  
  5. /*
  6.  *      Copyright (c) 1991 Purdue University Research Foundation,
  7.  *      West Lafayette, Indiana 47907.  All rights reserved.
  8.  *
  9.  *      Written by Victor A. Abell <abe@mace.cc.purdue.edu>,  Purdue
  10.  *      University Computing Center.  Not derived from licensed software;
  11.  *      derived from awf(1) by Henry Spencer of the University of Toronto.
  12.  *
  13.  *      Permission is granted to anyone to use this software for any
  14.  *      purpose on any computer system, and to alter it and redistribute
  15.  *      it freely, subject to the following restrictions:
  16.  *
  17.  *      1. The author is not responsible for any consequences of use of
  18.  *         this software, even if they arise from flaws in it.
  19.  *
  20.  *      2. The origin of this software must not be misrepresented, either
  21.  *         by explicit claim or by omission.  Credits must appear in the
  22.  *         documentation.
  23.  *
  24.  *      3. Altered versions must be plainly marked as such, and must not
  25.  *         be misrepresented as being the original software.  Credits must
  26.  *         appear in the documentation.
  27.  *
  28.  *      4. This notice may not be removed or altered.
  29.  */
  30.  
  31. #include <stdio.h>
  32. #ifdef UNIX
  33. #ifdef USG
  34. #include <string.h>
  35. #else
  36. #include <strings.h>
  37. #endif  
  38. #else
  39. #include <string.h>
  40. #endif
  41.  
  42. #define MAXLL   2048                    /* ridiculous maximum line length */
  43.  
  44. int Dash = 1;                           /* underline with dashes */
  45. int Dp = 0;                             /* dash pending */
  46. int Lc = 0;                             /* line count */
  47. char *Pname;                            /* program name */
  48. char Ulb[MAXLL];                        /* underline buffer */
  49. int Ulx = 0;                            /* underline buffer index */
  50.  
  51. void Putchar();
  52. #ifndef STDDEF
  53. char *strrchr();
  54. #endif
  55.  
  56. main(argc, argv)
  57.         int argc;
  58.         char *argv[];
  59. {
  60.         int ax = 1;                     /* argument index */
  61.         int c;                          /* character buffer */
  62.         FILE *fs;                       /* file stream */
  63.         int nf = 0;                     /* number of files processed */
  64.         char pc;                        /* previous character */
  65.         int under = 0;                  /* underline */
  66. /*
  67.  * Save program name.
  68.  */
  69.         if ((Pname = strrchr(argv[0], '/')) != NULL)
  70.                 Pname++;
  71.         else if ((Pname = strrchr(argv[0], '\\')) != NULL)
  72.                 Pname++;
  73.         else
  74.                 Pname = argv[0];
  75. /*
  76.  * Process options.
  77.  */
  78.         if (argc > 1 && argv[1][0] == '-') {
  79.                 switch (argv[1][1]) {
  80.         /*
  81.          * "-U" - underline with dashes.
  82.          */
  83.                 case 'U':
  84.                         Dash = 0;
  85.                         under = 1;
  86.                         break;
  87.         /*
  88.          * "-" - do no  underlining at all.
  89.          */
  90.                 case '\0':
  91.                         Dash = under = 0;
  92.                         break;
  93.                 default:
  94.                         (void) fprintf(stderr,
  95.                                 "%s usage: [-] [-U] [file]\n", Pname);
  96.                         exit(1);
  97.                 }
  98.                 ax++;
  99.         }
  100. /*
  101.  * Process files.  Read standard input if no files names.
  102.  */
  103.  
  104.         while (ax < argc || nf == 0) {
  105.                 if (ax >= argc)
  106.                         fs = stdin;
  107.                 else {
  108. #ifdef  UNIX
  109.                         if ((fs = fopen(argv[ax], "r")) == NULL)
  110. #else
  111.                         if ((fs = fopen(argv[ax], "rt")) == NULL)
  112. #endif
  113.                         {
  114.                                 (void) fprintf(stderr, "%s: can't open %s\n",
  115.                                         Pname, argv[ax]);
  116.                                 exit(1);
  117.                         }
  118.                         ax++;
  119.                 }
  120.                 nf++;
  121.         /*
  122.          * Read input a character at a time.
  123.          */
  124.                 for (pc = '\0'; (c = fgetc(fs)) != EOF;) {
  125.                         switch(c) {
  126.  
  127.                         case '\n':
  128.                                 if (pc)
  129.                                         Putchar(pc);
  130.                                 Putchar('\n');
  131.                                 pc = '\0';
  132.                                 break;
  133.  
  134.                         case '\b':
  135.                                 if (pc == '_') {
  136.                                         if (under) {
  137.                                                 putchar(pc);
  138.                                                 putchar('\b');
  139.                                         } else if (Dash)
  140.                                                 Dp = 1;
  141.                                 }
  142.                                 pc = '\0';
  143.                                 break;
  144.  
  145.                         default:
  146.                                 if (pc)
  147.                                         Putchar(pc);
  148.                                 pc = c;
  149.                         }
  150.                 }
  151.                 if (pc) {
  152.                         Putchar(pc);
  153.                         Putchar('\n');
  154.                 }
  155.         }
  156.         exit(0);
  157. }
  158.  
  159.  
  160. /*
  161.  * Putchar(ch) - put a character with possible underlining
  162.  */
  163.  
  164. void
  165. Putchar(ch)
  166.         char ch;
  167. {
  168.         int i;                                  /* temporary index */
  169.  
  170.         if (ch == '\n') {
  171. /*
  172.  * Handle end of line.
  173.  */
  174.                 putchar('\n');
  175.                 if (Ulx) {
  176.                         while (Ulx && Ulb[Ulx-1] == ' ')
  177.                                 Ulx--;
  178.                         if (Ulx) {
  179.                                 for (i = 0; i < Ulx; i++)
  180.                                         putchar(Ulb[i]);
  181.                                 putchar('\n');
  182.                         }
  183.                 }
  184.                 Dp = Ulx = 0;
  185.                 Lc++;
  186.                 return;
  187.         }
  188. /*
  189.  * Put "normal" character.
  190.  */
  191.         putchar(ch);
  192.         if (Dash) {
  193.  
  194.         /*
  195.          * Handle dash-type underlining.
  196.          */
  197.                 if (Ulx >= MAXLL) {
  198.                         (void) fprintf(stderr,
  199.                                 "%s: underline for line %d > %d characters\n",
  200.                                 Pname, Lc, MAXLL);
  201.                         exit(1);
  202.                 }
  203.                 Ulb[Ulx++] = Dp ? '-' : ' ';
  204.                 Dp = 0;
  205.         }
  206. }
  207.